package com.logistics.controller;

import com.alibaba.fastjson.JSONObject;
import com.logistics.conf.Configurator;
import com.logistics.entity.Goods;
import com.logistics.service.GoodsService;
import com.logistics.util.JsonUtil;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;


/**
 * Created by Mklaus on 15/8/16.
 */
@Controller
@RequestMapping("goods")
public class GoodsController {

    @Resource
    private GoodsService goodsService;

    @RequestMapping(method = RequestMethod.GET)
    public String goods_GET(Integer start,Integer size,Model model,HttpSession session){
        DetachedCriteria dc = DetachedCriteria.forClass(Goods.class);
        List<Goods> goodses = goodsService.get(dc,start,size);
        model.addAttribute("goodses",goodses);
        return "/goods/goodsShowAll";
    }

    @RequestMapping(method = RequestMethod.GET,params = "json")
    public void goods_GET_json(Integer start,Integer size,Model model,HttpSession session,HttpServletResponse resp) throws IOException {
        DetachedCriteria dc = DetachedCriteria.forClass(Goods.class);
        start = start != null ? start : 0;
        size  = size  != null ? size  : Configurator.getInstance().getInt("defaultPageSize");
        List<Goods> goodses = goodsService.get(dc,start,size);

        JSONObject json = new JSONObject();
        json.put("goodsesLength",goodsService.getAll().size());
        JsonUtil.write(goodses,"goodses",json,resp);
    }

    @RequestMapping(method = RequestMethod.POST)
    public String goods_CREATE(Goods goods,Model model,HttpSession session){
        goodsService.add(goods);
        model.addAttribute("goods",goods);

        return "/goods/goodsShow";
    }

    @RequestMapping(method = RequestMethod.PUT)
    public String goods_UPDATE(Goods goods,Model model,HttpSession session){
        goodsService.update(goods);
        model.addAttribute("goods",goods);

        return "/goods/goodsShow";
    }

    @RequestMapping(method = RequestMethod.DELETE)
    public String goods_DELETE(Integer goodsId){
        goodsService.delete(goodsId);
        return "redirect:/goods";
    }

    @RequestMapping(value = "{goodsId}",method = RequestMethod.GET)
    public String goods_id(@PathVariable("goodsId") Integer goodsId, Model model){
        Goods goods = goodsService.get(goodsId);
        model.addAttribute("goods",goods);
        return "/goods/goodsShow";
    }

    @RequestMapping(value = "{goodsId}",method = RequestMethod.GET,params = "json")
    public void goods_id_json(@PathVariable("goodsId") Integer goodsId,HttpServletResponse resp) throws IOException {
        Goods goods = goodsService.get(goodsId);
        JsonUtil.write(goods,resp);
    }


//    public void get() throws Exception{
//        int defaultPageSize = Configurator.getInstance().getInt("defaultPageSize");
//
//        HttpServletRequest req = ServletActionContext.getRequest();
//
//        Integer id          = getInt(req,"id");
//        String type         = getString(req,"type");
//        Date create_time    = getDate(req,"create_time");
//
//        Integer start       = getInt(req,"start");
//        Integer size        = getInt(req,"size");
//        size                = size != null ? size : defaultPageSize;
//
//        String order        = getString(req,"order");
//        String by           = getString(req,"by");
//
//        DetachedCriteria dc = DetachedCriteria.forClass(Goods.class);
//
//        if (id != null){
//            dc.add(Restrictions.eq("id", id));
//        }
//
//        if(type != null){
//            dc.add(Restrictions.eq("type",type));
//        }
//
//        if(create_time != null){
//            dc.add(Restrictions.le("create_time",create_time));
//        }
//
//        if ("asc".equals(order))
//            dc.addOrder(Order.asc(by));
//        if ("desc".equals(order))
//            dc.addOrder(Order.desc(by));
//
//        List<Goods> goodses = goodsService.get(dc,start,size);
//
//        JsonUtil.write(goodses, ServletActionContext.getResponse());
//    }

}
